package org.jeecg.modules.nurse_distance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.baidu.utils.BDMapUtil;
import org.jeecg.modules.nurse_distance.entity.NurseDistance;
import org.jeecg.modules.nurse_distance.mapper.NurseDistanceMapper;
import org.jeecg.modules.nurse_distance.service.INurseDistanceService;
import org.jeecg.modules.nurse_hospital.entity.NurseHospital;
import org.jeecg.modules.nurse_hospital.service.INurseHospitalService;
import org.jeecg.modules.nurse_user.entity.NurseUserAddr;
import org.jeecg.modules.nurse_user.service.INurseUserAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: nurse_distance
 * @Author: jeecg-boot
 * @Date: 2025-01-15
 * @Version: V1.0
 */
@Service
public class NurseDistanceServiceImpl extends ServiceImpl<NurseDistanceMapper, NurseDistance> implements INurseDistanceService {

    @Autowired
    private NurseDistanceMapper nurseDistanceMapper;

    @Autowired
    private INurseUserAddrService nurseUserAddrService;

    @Autowired
    private INurseHospitalService nurseHospitalService;

    /**
     * 通过地址Id获得交通费列表
     * @param addrId
     * @return
     */
    @Override
    public List<NurseDistance> getByAddrIdAndHospitalId(String addrId,String hospitalId) {
        NurseUserAddr nurseUserAddr = nurseUserAddrService.selectById(addrId);
        NurseHospital nurseHospital = nurseHospitalService.selectById(hospitalId);

        String longitudesStart = null; //起点经度
        String latitudeStart = null;  //起点纬度
        String longitudeEnd = null; //终点纬度
        String latitudeEnd = null; //终点纬度

        //用户地址经纬度
        if (nurseUserAddr != null) {
            longitudeEnd = nurseUserAddr.getLongitude();
            latitudeEnd = nurseUserAddr.getLatitude();

            if ((longitudeEnd == null || longitudeEnd.equals("")) || (latitudeEnd == null || latitudeEnd.equals("")) ) {
                Map<String, String> geocodingInfo;
                try {
                    geocodingInfo = BDMapUtil.getLocationByAddress(nurseUserAddr.getAreaName()+nurseUserAddr.getAddress());
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
                if (geocodingInfo.get("status").equals("-1")) {
                    return null;
                }
                longitudeEnd = geocodingInfo.get("lng");
                latitudeEnd = geocodingInfo.get("lat");
                nurseUserAddr.setLongitude(longitudeEnd);
                nurseUserAddr.setLatitude(latitudeEnd);
                nurseUserAddrService.updateById(nurseUserAddr);
            }
        }

        //医院地址经纬度
        if (nurseHospital != null) {
            longitudesStart = nurseHospital.getLongitude();
            latitudeStart = nurseHospital.getLatitude();

            if ((longitudesStart == null || longitudesStart.equals("")) || (latitudeStart == null || latitudeStart.equals("")) ) {
                Map<String, String> geocodingInfo;
                try {
                    geocodingInfo = BDMapUtil.getLocationByAddress(nurseHospital.getAddress());
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
                if (geocodingInfo.get("status").equals("-1")) {
                    return null;
                }
                longitudesStart = geocodingInfo.get("lng");
                latitudeStart = geocodingInfo.get("lat");
                nurseHospital.setLongitude(longitudesStart);
                nurseHospital.setLatitude(latitudeStart);
                nurseHospitalService.updateById(nurseHospital);
            }
        }

        if (longitudeEnd == null || longitudeEnd.equals("")) {
            return null;
        }else if (latitudeEnd == null || latitudeEnd.equals("")){
            return null;
        }else if (longitudesStart == null || longitudesStart.equals("")){
            return null;
        }else if (latitudeStart == null || latitudeStart.equals("")){
            return null;
        }

        Map params = new LinkedHashMap<String, String>();
        params.put("origin", latitudeStart + ","+longitudesStart); //起点经纬度
        params.put("destination", latitudeEnd + ","+longitudeEnd); //终点经纬度
        params.put("coord_type", "gcj02");

        Map<String, Object> geocodingInfo;
        try {
            geocodingInfo = BDMapUtil.getDirectionliteDriving(params);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        double distance = (double) geocodingInfo.get("distance"); //方案距离，单位：米
        double duration = (double) geocodingInfo.get("duration"); //线路耗时，单位：秒
        double toll = (double) geocodingInfo.get("toll"); //路线的过路费预估，单位：元

        //根据公里数获得远程费列表
        List<NurseDistance> NurseDistanceList = new ArrayList<NurseDistance>();
        NurseDistanceList.addAll(nurseDistanceMapper.getStartDistanceList()); //起步费
        NurseDistanceList.addAll(nurseDistanceMapper.getFixedDistanceList(distance)); //固定距离费
        NurseDistanceList.addAll(nurseDistanceMapper.getAdditionalDistanceList(distance)); //附加费

        return NurseDistanceList;
    }
}
